#include "testdriver.h"

TestDriver::TestDriver(RenderManager* r)
{
    rend=r;
}

void TestDriver::resTest(int i, bool res, Result r){
    string succ = " ";
    if (r.getResult()==res)
        succ = "succeeded";
    else {
        succ = "failed: ";
        succ = succ.append(r.getMessage());
    }
    ostringstream convert;
    convert << i;
    cout << "Test " << convert.str()<< " " << succ << "\n";
}

void TestDriver::runStdTests()
{
    //makeTestWorld(TestDriver::Test1);
    //testEqnParser();

}

void TestDriver::testEqnParser()
{
    printf("\nTesting Equation parser...\n");
    TEST(1, EqnParser("1/0").eval(0,0,0)>FLT_MAX );
    TEST(2, EqnParser("").eval(0,0,0) == 0 );
    TEST(3, EqnParser("sin(0)").eval(0,0,0) == 0. );
    TEST(4, EqnParser("cos(0)").eval(0,0,0) == 1. );
    TEST(5, EqnParser("tan(0)").eval(0,0,0) ==0. );
    TEST(6, EqnParser("sqrt(1)").eval(0,0,0) == 1. );
    TEST(7, EqnParser("fabs(-1)").eval(0,0,0) == 1. );
    TEST(8, EqnParser("x").eval(1,0,0) == 1. );
    TEST(9, EqnParser("y").eval(0,1,0) == 1. );
    TEST(10, EqnParser("z").eval(0,0,1) == 1. );
    TEST(11, EqnParser("(x)").eval(1,0,0) == 1. );
    //TEST(12, 0);
    //printf("%f\n",EqnParser("tan(0)").eval(0,0,0));
}

void TestDriver::testBrush()
{
    printf("\nTesting brush\n");
    BrushToolATD brushTest = BrushToolATD();
    brushTest.testProperties();
    brushTest.testEvents();
    brushTest.testShape();
    brushTest.setProperties(10,5,5,BrushUtilities::SPHERE); // set back to default
}

void TestDriver::testSelect(){
    printf("\nTesting select\n");
    SelectionToolATD selectTest = SelectionToolATD();
    selectTest.testActiveSelection();
    selectTest.testValidCoords();
    selectTest.testMode();
    selectTest.testAction();

}

void TestDriver::testMagicWand(){
    printf("\nTesting magic wand\n");
    MagicBrushATD magicTest = MagicBrushATD();
    magicTest.testRGB();
    magicTest.testSelectedVoxel();
    magicTest.testContinguous();
    magicTest.testNonContiguous();
    magicTest.testVoxelInSelection();
}

float TestDriver::Test1(float x, float z)
{
    //     return (sin(x*0.1)*cos(z*0.1))*60+60;
    //    x-=128;
    //    z-=128;

    //    x*=0.3;
    //    z*=0.3;
    //    return 20*(sin(sqrt(x*x+z*z)))+ 20;
    return 0;
}

void TestDriver::makeTestWorld(float (*func)(float x, float z))
{
    voxel v;
    v.setColor(0,66,200);
    //rend->addVoxel(0,0,1,v);
    srand(time(NULL));
    for(int i=1;i<X_CHUNKS*CHUNK_SIZE;i++)
        for(int j=1;j<Z_CHUNKS*CHUNK_SIZE;j++){
            int k=func(i,j);
            int l,diff;

            l=func(i-1,j);
            diff=k-l;
            for(int s=0;s<=10;s++){
                int t=l+diff*(s/10.);
                v.setColor(colour(rand()%256,rand()%256,rand()%256));
                if(t<X_CHUNKS*CHUNK_SIZE && t>=0)
                    rend->addVoxel(i-1,t,j,v);

            }

            l=func(i,j-1);
            diff=k-l;
            for(int s=0;s<=10;s++){
                int t=l+diff*(s/10.);
                v.setColor(colour(rand()%256,rand()%256,rand()%256));
                if(t<X_CHUNKS*CHUNK_SIZE && t>=0)
                    rend->addVoxel(i,t,j-1,v);
            }
        }

}

